/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.AlunoDao;
import Dao.AlunoDaoImplJPA;
import Dao.DisciplinaDao;
import Dao.DisciplinaDaoImplJPA;
import Dao.HistoricoDao;
import Dao.HistoricoDaoImplJPA;
import Pojo.Aluno;
import Pojo.Disciplina;
import Pojo.Historico;
import java.awt.Color;
import java.util.List;

/**
 *
 * @author thiago
 */
public class FormConsultaSituacaoAlunoDisciplina extends javax.swing.JInternalFrame {

    /**
     * Acesso necessário para o caso de chamar o cadastro de professore
     */
    private int acesso;
    
    /**
     * Objeto professor para acesso.
     */
    private Aluno alunoAcesso;

    public int getAcesso() {
        return acesso;
    }

    private void setAcesso(int acesso) {
        this.acesso = acesso;
    }

    public Aluno getAlunoAcesso() {
        return alunoAcesso;
    }

    private void setAlunoAcesso(Aluno alunoAcesso) {
        this.alunoAcesso = alunoAcesso;
    }
    
    
    /**
     * Creates new form FormConsultaSituacaoAlunoDisciplina
     */
    public FormConsultaSituacaoAlunoDisciplina(int acesso, Aluno aluno) {
        initComponents();
        this.setAcesso(acesso);
        this.setAlunoAcesso(aluno);
        if (this.getAcesso()==3){
            cbbAluno.setSelectedItem(this.getAlunoAcesso().getId()+" - "+this.getAlunoAcesso().getNome());
            cbbAluno.setEnabled(false);
        }else{
            this.buscarAlunos();
        }
        this.buscarDisciplinas();
        txtCodigo.setEditable(false);
        txtAluno.setEditable(false);
        txtDisciplina.setEditable(false);
        txtFaltas.setEditable(false);
        txtNota.setEditable(false);
        txtSituacao.setEditable(false);
    }
    
    /**
     * busca de alunos
     */
    private void buscarAlunos(){
        AlunoDao alunoDao = new AlunoDaoImplJPA();
        List<Aluno> listaAlunos = alunoDao.pesquisarTodos();
        
        for(Aluno aluno : listaAlunos){
            cbbAluno.addItem(aluno.getId()+" - "+aluno.getNome());
        }
    }

    private void buscarDisciplinas(){
        DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
        List<Disciplina> listaDisciplinas = disciplinaDao.pesquisarTodos();
        
        for(Disciplina disciplina : listaDisciplinas){
            cbbDisciplina.addItem(disciplina.getId()+" - "+disciplina.getNome());
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbbAluno = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbbDisciplina = new javax.swing.JComboBox();
        btnBuscarDados = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtAluno = new javax.swing.JTextField();
        txtDisciplina = new javax.swing.JTextField();
        txtNota = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtFaltas = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtSituacao = new javax.swing.JTextField();

        setClosable(true);
        setTitle("Consulta da Situação do Aluno na Disciplina");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Aluno");

        jLabel2.setText("Disciplina");

        btnBuscarDados.setText("Buscar Dados");
        btnBuscarDados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarDadosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnBuscarDados))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbbDisciplina, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbbAluno, javax.swing.GroupLayout.Alignment.TRAILING, 0, 330, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbbAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbbDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuscarDados)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setText("Código");

        jLabel4.setText("Aluno");

        jLabel5.setText("Disciplina");

        jLabel6.setText("Nota");

        jLabel7.setText("Faltas");

        jLabel8.setText("Situação");

        txtSituacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSituacaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFaltas, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .addComponent(txtNota, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(12, 12, 12))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtSituacao, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDisciplina))
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(txtAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(txtNota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(txtFaltas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(txtDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(txtSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inserirDadosCampos(String codigo, String alunoNome, 
                                    String disciplinaNome, String nota, 
                                    String faltas, String situacao){
        txtCodigo.setText(codigo);
        txtAluno.setText(alunoNome);
        txtDisciplina.setText(disciplinaNome);
        txtNota.setText(nota);
        txtFaltas.setText(faltas);
        txtSituacao.setText(situacao);
    }
    
    private void btnBuscarDadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarDadosActionPerformed
        String disciplinaString = (String) cbbDisciplina.getSelectedItem();
        String alunoString;
        String[] dados;
        AlunoDao alunoDao = new AlunoDaoImplJPA();
        DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
        HistoricoDao historicoDao = new HistoricoDaoImplJPA();
        
        if (disciplinaString.length()>0){
            Aluno aluno;
            if (this.getAcesso() == 3){
                aluno = this.getAlunoAcesso();
            }else{
                alunoString = (String) cbbAluno.getSelectedItem();
                if (alunoString.length()>0){
                    dados = alunoString.split(" - ");
                }else{
                    return;
                }
                aluno = alunoDao.pesquisarPorId(Integer.parseInt(dados[0]));
            }
            
            dados = disciplinaString.split(" - ");
            Disciplina disciplina = disciplinaDao.pesquisarPorId(Integer.parseInt(dados[0]));
            Historico historico = historicoDao.pesquisarPorAlunoEDisciplina(aluno, disciplina);
            if (historico == null){
                this.inserirDadosCampos("", "", "", "", "", "Aluno nunca cursou a disciplina");
                txtSituacao.setForeground(Color.red);
            }else{
                this.inserirDadosCampos(String.valueOf(historico.getId()), aluno.getNome(), disciplina.getNome(), String.valueOf(historico.getNotaFinal()), String.valueOf(historico.getNumeroDeFaltas()), "Aluno aprovado na disciplina");
                txtSituacao.setForeground(Color.blue);
            }
        }
    }//GEN-LAST:event_btnBuscarDadosActionPerformed

    private void txtSituacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSituacaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSituacaoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarDados;
    private javax.swing.JComboBox cbbAluno;
    private javax.swing.JComboBox cbbDisciplina;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtAluno;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDisciplina;
    private javax.swing.JTextField txtFaltas;
    private javax.swing.JTextField txtNota;
    private javax.swing.JTextField txtSituacao;
    // End of variables declaration//GEN-END:variables
}
